package com.wealth.sms.web.controller;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.sms.entity.MessageChannel;
import com.wealth.sms.service.IMessageChannelService;
import com.wealth.system.entity.Corporation;
import com.wealth.system.service.ShiroUser;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.RedirectMessage;
import iaas.web.JqGridRequest;
import iaas.web.JqGridResponse;
import iaas.web.JqGridUtil;


/**
 * 短信通道管理
 * @author chan
 */
@Controller
@RequestMapping(value="/manage/sms/messageChannel")
public class MessageChannelController  extends BaseController{

	Logger logger = Logger.getLogger(MessageChannelController.class);
	
	@Autowired
	private IMessageChannelService messageChannelService;
	
	
	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Classify对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void init(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		try{
			if (id != -1) {
				MessageChannel chanel = messageChannelService.get(id);
				model.addAttribute("chanel", chanel);
			}
			
		}catch(Exception e){
			logger.error("短信通道初始化错误", e);
		}
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String toList(Model model){
		return "sms/messageChannel/list";
	}
	
	
	/**
	 * 查询数据
	 */
	@RequestMapping(value="/list",method = RequestMethod.POST)
	@ResponseBody
	public JqGridResponse getJson(Model model,JqGridRequest request) {
		try{
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			Page<MessageChannel> page = messageChannelService.get(corpid, request,MessageChannel.class);
			return JqGridUtil.getJqGrirdResponse(page);
		}catch(Exception e){
			logger.error("列表错误！", e);
			return null;
		}
	}
	/**
	 * 创建短信通道页面
	 */
	@RequestMapping(value="/create",method = RequestMethod.GET)
	public String createForm(Model model,RedirectAttributes redirectAttributes){
		try {
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			MessageChannel channel = messageChannelService.findMessageChannelByCorpId(corpid);
			if(channel!=null){
				redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipWarning("企业只能配置一条短信通道"));
				return "redirect:/manage/sms/messageChannel/list";
			}
			return "sms/messageChannel/create";
		} catch (Exception e) {
			logger.error("创建短信通道错误", e);
			return "system/500";
		}
	}
	
	/**
	 * 创建短信通道
	 */
	@RequestMapping(value="/create",method = RequestMethod.POST)
	public String create(Model model,MessageChannel entity,RedirectAttributes redirectAttributes){
		try {
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			
			entity.setCorp(new Corporation(corpid));
			entity.setCreator(ShiroUser.getInstance().getUser().getUsername());
			entity.setCreatetime(new Date());
			entity.setStatus(StatusEnum.NORMAL.getValue());
			messageChannelService.save(entity);
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess("创建短信通道成功"));
			return "redirect:/manage/sms/messageChannel/list";
		} catch (Exception e) {
			logger.error("创建短信通道错误", e);
			return "system/500";
		}
	}
	
	/**
	 * 编辑短信通道页面
	 */
	@RequestMapping(value="/update/{id}",method = RequestMethod.GET)
	public String setUpdateForm(Model model,@PathVariable("id") Long id){
		try{
			MessageChannel messageChannel = messageChannelService.get(id);
			model.addAttribute("messageChannel", messageChannel);
			return "sms/messageChannel/update";
		}catch(Exception e){
			logger.error("编辑短信通道页面错误,id="+id, e);
			return "system/500";
		}
	}
	
	/**
	 * 修改短信通道信息
	 */
	@RequestMapping(value="/update/{id}",method = RequestMethod.POST)
	public String update(@PathVariable("id") Long id,@ModelAttribute("chanel") MessageChannel chanel,RedirectAttributes redirectAttributes){
		try{
			messageChannelService.clear();
			messageChannelService.save(chanel);
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess("编辑短信通道成功"));
			return "redirect:/manage/sms/messageChannel/list";
		}catch(Exception e){
			logger.error("编辑短信通道错误,id="+id, e);
			return "system/500";
		}
	}
	
	/**
	 * 删除短信通道（逻辑删除）
	 */
	@RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
	public String delete(@PathVariable Long id,RedirectAttributes redirectAttributes){
		try{
			messageChannelService.deleteLogical(id);
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess("删除短信通道成功"));
			return "redirect:/manage/sms/messageChannel/list";
		}catch(Exception e){
			logger.error("删除短信通道错误,id="+id,e);
			return "system/500";
		}
	}
	
	/**
	 * 停用短信通道
	 */
	@RequestMapping(value="/disable/{id}",method = RequestMethod.GET)
	public String ban(@PathVariable Long id,RedirectAttributes redirectAttributes){
		try{
			messageChannelService.updateStatus(id, StatusEnum.STOP.getValue());
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipWarning("停用短信通道成功"));
			return "redirect:/manage/sms/messageChannel/list";
		}catch(Exception e){
			logger.error("停用短信通道错误,id="+id,e);
			return "system/500";
		}
	}
	
	@RequestMapping(value="/enable/{id}",method = RequestMethod.GET)
	public String enable(@PathVariable Long id,RedirectAttributes redirectAttributes){
		try{
			messageChannelService.updateStatus(id, StatusEnum.NORMAL.getValue());
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipWarning("启用短信通道成功"));
			return "redirect:/manage/sms/messageChannel/list";
		}catch(Exception e){
			logger.error("启用短信通道错误,id="+id,e);
			return "system/500";
		}
	}
	
	
}
